﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PaperAnimCtrl : MonoBehaviour
{
    private Unit _unit;
    private Material _material;
    private MeshFilter _meshFilter;
    private bool _initOK;
    private bool _canPlay;
    private bool _needLoop;
    private float _speed;
    private float _rowNum;
    private float _columnNum;
    private float _needShowPicIndex;
    private float _timer;
    private float _offsetX;
    private float _offsetY;
    private float _totalPicNum;
    private bool _needCheck;
    private int _needCheckFrame;
    private bool _needCheckFinish;
    private bool _hasCheckFrame = false;
    private bool _hasCheckFinish = false;

    public float RowNum
    {
        get { return _rowNum; }
    }

    public void Init(Unit unit)
    {
        _unit = unit;
    }

    void FixedUpdate()
    {
        if (!BattleStage.g_InBattle)
            return;

        if (_canPlay)
        {
            _timer += Time.fixedDeltaTime;
            if (_timer >= 1f / _speed)
            {
                _needShowPicIndex += 1;
                _timer = 0;
                _hasCheckFrame = false;
                _hasCheckFinish = false;
            }
            _needShowPicIndex = _needShowPicIndex % _totalPicNum;
            _offsetX = (_needShowPicIndex / _columnNum) % 1;
            _offsetY = 1 - 1 / _rowNum * (1 + Mathf.Floor(_needShowPicIndex / _columnNum));
            Vector2[] uvList = new Vector2[4];
            uvList[0] = new Vector2(_offsetX, _offsetY);
            uvList[2] = new Vector2(_offsetX + 1f / _columnNum, _offsetY);
            uvList[3] = new Vector2(_offsetX, _offsetY + 1f / _rowNum);
            uvList[1] = new Vector2(_offsetX + 1f / _columnNum, _offsetY + 1f / _rowNum);
            _meshFilter.mesh.uv = uvList;
            if ( !_hasCheckFrame && _needCheck && _needShowPicIndex == _needCheckFrame)
            {
                 _unit.OnActionFrameCheck();
                _hasCheckFrame = true;
            }
            if (_needShowPicIndex == (_totalPicNum - 1))
            {
                if (!_needLoop)
                {
                    _canPlay = false;
                }
                if (!_hasCheckFinish && _needCheckFinish)
                {
                     _unit.OnActionFinishCheck();
                    _hasCheckFinish = true;
                }
            }
        }
    }

    public void ResetAnim(bool needLoop, Material mat,bool needCheck, float needCheckFrame,bool needCheckFinish)
    {
        _needCheck = needCheck;
        _needCheckFrame = (int)needCheckFrame;
        _needCheckFinish = needCheckFinish;
        ResetMaterial(needLoop, mat);
    }

    public void RandomChangeActionFrameOrSpeed(int minNum, int naxNum, float speedChangeNum)
    {
        _needShowPicIndex += Random.Range(minNum, naxNum);
        _speed += Random.Range(speedChangeNum * _speed * (-1f), speedChangeNum * _speed);
    }

    public void ResetMaterial(bool needLoop, Material mat)
    {
        if (_initOK == false)
        {
            _meshFilter = this.GetComponent<MeshFilter>();
            _initOK = true;
        }
        _material = mat;
        _rowNum = _material.GetFloat("_SizeX");
        _columnNum = _material.GetFloat("_SizeY");
        _speed = _material.GetFloat("_Speed") ;
        _totalPicNum = _material.GetFloat("_ImageNum");
        float loopNum = _material.GetFloat("_LoopNum");
        if (needLoop == false)
        {
            _needLoop = false;
        }
        else
        {
            _needLoop = (loopNum == -1);
        }
        _timer = 0;
        _needShowPicIndex = 0;
        _offsetX = (_needShowPicIndex / _columnNum) % 1;
        _offsetY = 1 - 1 / _rowNum * (1 + Mathf.Floor(_needShowPicIndex / _columnNum));
        Vector2[] uvList = new Vector2[4];
        uvList[0] = new Vector2(_offsetX, _offsetY);
        uvList[2] = new Vector2(_offsetX + 1f / _columnNum, _offsetY);
        uvList[3] = new Vector2(_offsetX, _offsetY + 1f / _rowNum);
        uvList[1] = new Vector2(_offsetX + 1f / _columnNum, _offsetY + 1f / _rowNum);
        _meshFilter.mesh.uv = uvList;
        _canPlay = true;
    }
}
